local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script,ClientOHOH.files.BaseObject(),"PetGoldGui_"..script.Name)
local CallBackManager = ClientOHOH.load.CallBackManager();
local ConnectClass=_G.CommonOHOH.load.Connect();
local PetTool=_G.CommonOHOH.load.PetTool();
local initStr = {
    petList = "ctrl.petList.petList",
    boost = "ctrl.goldBoost"
}

function M.new(...)
    M(...)
end


function M.ctor(self,ctrl)
    M.super.ctor(self);
    self.ctrl=ctrl
    self.List = {} --实例列表
    self.PetInfoList = {}
    self.ui=self.ctrl:InitUiInst2(self.ctrl.main_inst,initStr)
    self:ClearCell()
    self.selectedCount = 0
    self.SelectedId = 0 --petid
    self.isHundredPercent = false
    self:AddConnect()
    self:SetBoostText()
end

function M.AddConnect(self)
    self.ctrl.DatabaseReadyEvent:Connect(function()
        self.database = self.ctrl:GetDataBase()
        self:InitPets()
    end)

    self.ctrl.isShowEvent:Connect(function(isShow)
        if isShow then
            self:ShowPets()
        else
            self:Reset()
        end
    end)

    self.ctrl.isHundredPercentEvent:Connect(function(isHundred)
        self.isHundredPercent = isHundred
    end)

    self.ctrl.MakeSuccessEvent:Connect(function()
        self:InitPets()
    end)

    --制作更新事件
    self.ctrl.MakeEvent:Connect(function(petId,count)
        for _, value in pairs(self.List) do
            if value.petId == petId and count ~= 0 and value.Visible == true then
                value.Visible = false
                count = count -1
            end
        end
        self.selectedCount = 0
        self.SelectedId = 0
        self.ctrl.PetSelectedEvent:Action(self.selectedCount,self.SelectedId)
    end)

    --初始选中事件
    self.ctrl.InitSelectEvent:Connect(function(petId)
        self:SimulateClick(petId)
    end)
end

function M.CreateCell(self,guid,petId)
    local cell = ClientOHOH.load.PetCell().new(petId,guid,self.ui.petList,self.database)
    cell.ClickEvent:Connect(function()
        self:CellClick(cell)
    end)
    return cell
end

function M.CellClick(self,cell)
    if self.isHundredPercent and not cell:isSelected() then
        return
    end
    cell:SetSelectedStatus(not cell:isSelected())
    local isSelected = cell:isSelected()
    if isSelected then
        self.selectedCount = self.selectedCount + 1
        self.SelectedId = cell:GetPetId()
        if self.selectedCount == 1 then
            self:ShowPets()
        end
    else
        self.selectedCount = self.selectedCount - 1
        if self.selectedCount==0 then
            self.SelectedId = 0
            self:ShowPets()
        end
    end
    self.ctrl.PetSelectedEvent:Action(self.selectedCount,self.SelectedId)
end



function M.GetGoldenPetIdList(self)
    local pets = self.database:GetData("pets")
    table.clear(self.PetInfoList)
    for key, value in pairs(pets.had) do
        --key是petId,value是数量
        if PetTool.IsCanGold(key) then
            local data = {
                petId = key,
                amount = value,
                guid = 0
            }
            table.insert(self.PetInfoList,data)
        end
    end
end

function M.InitPets(self)
    self:GetGoldenPetIdList(self)
    local index = 0
    table.clear(self.List)
    self:ClearCell()
    for _, value in pairs(self.PetInfoList) do
        for i = 1, value.amount, 1 do
            index = index + 1
            value.guid = self:GetGuid(value.petId,index)
            self.List[index] = self:CreateCell(value.guid,value.petId)
        end
    end
end

--根据当前选中个数显示pet列表
function M.ShowPets(self)
    if self.selectedCount == 0 then
        for _, cell in pairs(self.List) do
            cell:SetShowStatus(true)
        end
    else
        for _, cell in pairs(self.List) do
            if cell:GetPetId() == self.SelectedId then
                cell:SetShowStatus(true)
            else
                cell:SetShowStatus(false)
            end
        end
    end
end


--清除petlist下的所有frame
function M.ClearCell(self)
    local children = self.ui.petList:GetChildren()
    if children then
        for _, child in pairs(children) do
            if child:IsA("Frame") then
                child:Destroy()
            end
        end
    end
end

function M.Reset(self)
    self.selectedCount = 0
    self.SelectedId = 0
    self.ctrl.PetSelectedEvent:Action(self.selectedCount,self.SelectedId)
    for _, cell in pairs(self.List) do
       cell:SetSelectedStatus(false)
    end
end


function M.GetGuid(self,petId,index)
    return petId.."_"..index
end

--在宠物界面选中点击后进入自动选择
function M.SimulateClick(self,petId)
    for _, value in pairs(self.List) do
        if value.petId == petId then
            self:CellClick(value)
            return
        end
    end
end

function M.SetBoostText(self)
    local boost = PetTool.GetEvolutionBoostStr(2).."%%"
    self.ctrl:FullText(self.ui.boost,{boost})
end

function M.dtor(self)
	M.super.dtor(self)
end

return M